package com.xca.znb.view;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.xca.znb.entity.News;
import com.xca.znb.entity.User;
import com.xca.znb.model.impl.ShowNewsImpl;
import com.xca.znb.presenter.ShowNewsPresenter;

public class SchoolShowNews extends AppCompatActivity {
    private ImageView  back  ;
    private CheckBox addCollection ;
    private WebView showNews ;
    private ShowNewsPresenter presenter ;
    @SuppressLint({"MissingInflatedId", "WrongViewCast"})
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_news);
        back = findViewById(R.id.show_news_back) ;
        addCollection = findViewById(R.id.show_news_add_collection);
        showNews = findViewById(R.id.show_news_message) ;

        ShowNewsImpl showNews1 = new ShowNewsImpl(this) ;
        presenter = new ShowNewsPresenter(showNews1);

        Intent intent = getIntent();
        String getNewsId = intent.getStringExtra("schoolFragment_showNews");

        try {
            int newsId = Integer.parseInt(getNewsId);
            News news = presenter.getNewsById(newsId);
            User user = presenter.getUserByPhone(news.getPublisher());

            WebSettings webSettings = showNews.getSettings();
            webSettings.setJavaScriptEnabled(true);
            webSettings.setLoadWithOverviewMode(true);
            webSettings.setUseWideViewPort(true);

// 假设我们要加载ID为1的新闻
            String htmlContent = buildHtml(news,user);
            showNews.loadDataWithBaseURL(null, htmlContent, "text/html", "utf-8", null);
        } catch (NumberFormatException e) {
            // 处理异常，例如设置默认值或者显示错误信息
            e.printStackTrace();
        }
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }

    private String buildHtml(News news , User user) {
        StringBuilder htmlBuilder = new StringBuilder();
            htmlBuilder.append("<html><head><style>")
                    .append("body {\n" +
                            "            font-family: Arial, sans-serif;\n" +
                            "            line-height: 1.6;\n" +
                            "        }\n" +
                            "        .header {\n" +
                            "            text-align: center;\n" +
                            "            margin-bottom: 20px;\n" +
                            "            font-size: 35px;\n" +
                            "        }\n" +
                            "        .sub-header {\n" +
                            "            color: #666;\n" +
                            "            margin-bottom: 10px;\n" +
                            "            font-size: 50px;" +
                            "        }\n" +
                            "        .metadata {\n" +
                            "            color: #999;\n" +
                            "            text-align: right;\n" +
                            "            margin-bottom: 10px;\n" +
                            "            font-size: 35px;" +
                            "        }\n" +
                            "        img {\n" +
                            "            max-width: 100%;\n" +
                            "            height: auto;\n" +
                            "            display: block;\n" +
                            "            margin: 0 auto;\n" +
                            "            margin-bottom: 20px;\n" +
                            "            width: 100%;\n" +
                            "            height: 100px;" +
                            "        }\n" +
                            "        .article-content {\n" +
                            "            text-align: justify;\n" +
                            "            font-size: 45px;" +
                            "        }\n" +
                            "    </style>\n" +
                            "</head>")
                    .append("<body>")
                    .append("<div class='header'><h1>").append(news.getFirstTitle()).append("</h1></div>")
                    .append("<p class='sub-header'>").append(news.getSecondTitle()).append("</p>")
                    .append("<p class='metadata'>发布时间: ").append(news.getTime()).append("</p>");
                    if (user.getName() != null){
                        htmlBuilder.append("<p class='metadata'>").append(" 发布者: ").append(user.getName()).append("</p>");
                    }else {
                        htmlBuilder.append("<p class='metadata'>").append(" 发布者: ").append(news.getPublisher()).append("</p>");
                    }
                    htmlBuilder.append("<img class='image' src='").append(news.getImg()).append("' alt='新闻图片'>") // 图片路径
                    .append("<div class='article-content'>").append("&nbsp;&nbsp;&nbsp;").append(news.getTexts()).append("</div>")
                    .append("</body></html>");
        return htmlBuilder.toString();
    }
}
